package com.gosophia.reportData.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gosophia.commons.OrderCondition;
import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.PropertyFilter.OperatorType;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.holidayManagement.ws.HolidayManagementWebService;
import com.gosophia.reportData.ws.LaunchReportWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;

/**
 * 审核任务列表
 * 
 * @author tianchunyu
 * 
 */
@Component
@Scope("prototype")
public class ListApprovalingAjaxAction extends BaseAjaxAction<JobDTO> {

	private String remind;
	private String warn;
	private String noState;
	private String jobState;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private LaunchReportWebService launchReportWebService;
	@Autowired
	private HolidayManagementWebService holidayManagementWebService;

	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		// 高级查询
		if (searchType) {
			if ("0".equals(remind) && "1".equals(warn) && noState == null) {
				// 提醒
				Date date = holidayManagementWebService.findTheAfterDay(
						new Date(), 2);
				PropertyFilter isNull = new PropertyFilter();
				isNull.setPropertyName("actualEndTime");
				isNull.setOperator(OperatorType.IS_NULL);
				PropertyFilter scheduleGE = new PropertyFilter(
						"scheduleEndDate", OperatorType.GE, new Date());
				PropertyFilter scheduleLE = new PropertyFilter(
						"scheduleEndDate", OperatorType.LE, date);
				isNull.and(scheduleGE.and(scheduleLE));
				// 预警
				PropertyFilter p = new PropertyFilter("actualEndTime",
						OperatorType.GT, "${scheduleEndDate}");
				PropertyFilter isNull2 = new PropertyFilter();
				isNull2.setPropertyName("actualEndTime");
				isNull2.setOperator(OperatorType.IS_NULL);
				p.or(isNull2.and(new PropertyFilter("scheduleEndDate",
						OperatorType.LT, new Date())));
				// 或
				isNull.or(p);
				propertyFilters.add(isNull);
			}
			if ("0".equals(remind) && warn == null && noState == null) {
				Date date = holidayManagementWebService.findTheAfterDay(
						new Date(), 2);
				PropertyFilter isNull = new PropertyFilter();
				isNull.setPropertyName("actualEndTime");
				isNull.setOperator(OperatorType.IS_NULL);
				PropertyFilter scheduleGE = new PropertyFilter(
						"scheduleEndDate", OperatorType.GE, new Date());
				PropertyFilter scheduleLE = new PropertyFilter(
						"scheduleEndDate", OperatorType.LE, date);
				isNull.and(scheduleGE.and(scheduleLE));
				propertyFilters.add(isNull);
			}
			if ("1".equals(warn) && remind == null && noState == null) {
				PropertyFilter p = new PropertyFilter("actualEndTime",
						OperatorType.GT, "${scheduleEndDate}");
				PropertyFilter isNull2 = new PropertyFilter();
				isNull2.setPropertyName("actualEndTime");
				isNull2.setOperator(OperatorType.IS_NULL);
				p.or(isNull2.and(new PropertyFilter("scheduleEndDate",
						OperatorType.LT, new Date())));
				propertyFilters.add(p);

			}
			if ("0".equals(jobState)) {
				PropertyFilter p = new PropertyFilter("startDate",
						OperatorType.LT, "${previousJob.scheduleEndDate}");
				propertyFilters.add(p);
			} else if ("1".equals(jobState)) {
				PropertyFilter p = new PropertyFilter("startDate",
						OperatorType.GT, "${previousJob.scheduleEndDate}");
				propertyFilters.add(p);
			}
			this.page = launchReportWebService.findApprovalDataADVByPage(page
					.getPageNo(), page.getPageSize(), propertyFilters,
					conversionOrder(orderConditions), userInfoId);
		} else {
			this.page = launchReportWebService.findApprovalDataByPage(page
					.getPageNo(), page.getPageSize(), searchString,
					propertyFilters, conversionOrder(orderConditions), userInfoId);
		}

		super.getRequestResult().setRsltCode("0");

		return SUCCESS;
	}

	private List<OrderCondition> conversionOrder(List<OrderCondition> source) {
		List<OrderCondition> target = null;
		if (source != null && source.size() > 0) {
			target = new ArrayList<OrderCondition>();
			for (OrderCondition order : source) {
				if ("jobState".equals(order.getPropertyName())) {
					if (order.isASC()) {
						target
								.add(OrderCondition
										.asc("${previousJob.actualEndTime} - ${previousJob.scheduleEndDate}"));
					} else {
						target
								.add(OrderCondition
										.desc("${previousJob.actualEndTime} - ${previousJob.scheduleEndDate}"));
					}
				} else {
					target.add(order);
				}
			}
			return target;
		}
		return null;
	}

	public JobDTO getDetailData() {
		return null;
	}

	public String getRemind() {
		return remind;
	}

	public void setRemind(String remind) {
		this.remind = remind;
	}

	public String getWarn() {
		return warn;
	}

	public void setWarn(String warn) {
		this.warn = warn;
	}

	public String getNoState() {
		return noState;
	}

	public void setNoState(String noState) {
		this.noState = noState;
	}

	public String getJobState() {
		return jobState;
	}

	public void setJobState(String jobState) {
		this.jobState = jobState;
	}

}
